// JavaScript Document

$(function(){
	//头部搜索功能
	var search_btn = $('#search-btn');
	if (search_btn.length) {
		search_btn.on('click',function(){
			var key = $('#search-key').val();
			if (key != '') {
				window.location.href = "index.php?m=Search&c=Index&a=index&key="+key;
			};
		});
	};

	$("[data-href='checkbox']").click(function(e){
		$(this).parents("table").find("input[type=checkbox]").attr('checked',this.checked);
	})

    var confirm_btn = $('.del-confirm-btn');
    if (confirm_btn.length) {
        //评论删除确认
        $('.del-confirm-btn').on('click', function(e){
            e.preventDefault();
            var obj = $(this);
            Wind.Util.ajaxConfirm({
                elem : obj,
                href : this.href,
                msg : '真的删了噢？',
                callback : function(data){
                    Wind.Util.resultTip({
                        error : true,
                        msg : data.data ? data.data : (data.info ? data.info : data.message),
                        follow : obj,
                        callback : function(){
                            location.reload();
                        }
                    });
                }
            });
        });
    };

    //点击ajax提交操作功能绑定
    var J_ajax_action = $('a.J_ajax_action');
    if (J_ajax_action.length) {
    	J_ajax_action.on('click',function(){
    		var obj = $(this);
			$.ajax({
				url    : obj.attr('href'),
				type   : 'POST',
				success  : function(data){
					Wind.Util.resultTip({
						error : true,
						msg : data.data ? data.data : (data.info ? data.info : data.message),
						follow : obj,
						callback : function(){
							if (data.url) {
								window.location.href = data.url;
							}else{
								location.reload();
							}
						}
					});
				},
				dataType : 'json',
			});
			return false;
    	})
    };

	//省市区三级联动操作
	var J_province = $('.J_province');
	var J_city = $('.J_city');
	var J_area = $('.J_area');
	if (J_province.length) {
		//初始化数据
		setAreaOption(J_province,0);
		if(J_city.attr('data-init')){
			setAreaOption(J_city,J_province.attr('data-init'));
		}
		if(J_area.attr('data-init')){
			setAreaOption(J_area,J_city.attr('data-init'));
		}
		//触发改变值的操作
		J_province.on('change',function(){
			setAreaOption(J_city,J_province.val());
		})
		J_city.on('change',function(){
			setAreaOption(J_area,J_city.val());
		})
	};

	function setAreaOption(obj,parentid){
		var init = obj.attr('data-init');
		obj.html('<option value="">请选择</option>');
		$.ajax({
			url      : 'index.php?m=Msic&c=Area&a=area&id='+parentid,
			success  : function(data){
				var info = data.info;
				var text = '<option value="">请选择</option>';
				if (data.status == 1) {
					$(info).each(function(i,rs){
						if (init == rs['areaid']) {
							text = text + '<option value="'+rs['areaid']+'" selected>'+rs['name']+'</option>';
						}else{
							text = text + '<option value="'+rs['areaid']+'">'+rs['name']+'</option>';
						}
					})
					obj.html(text);
				};
			},
			dataType : 'json',
		})
	}


	//处理此时此刻图片展示问题
	var content_image = $('.article-show .content');
	if (content_image.length) {
		var img_list = content_image.find('img');
		if (img_list.length) {
			$(img_list).each(function(){
				var width = $(this).width();
				var height = $(this).height();
				if (width > 550) {
					$(this).width(550).height((550/width)*height).wrap("<span style='display:block'></span>");
				};
			});
		};
	};
})

function UploadParse(){
	$('body').append("<div id='upload_ue' style='display:none'></div>");
	//上传独立使用
	var _editor = UE.getEditor('upload_ue');
	_editor.ready(function () {
		_editor.setDisabled(['insertimage','insertfile']);
        _editor.hide();
	});
	function upImage(obj) {
		var myImage = _editor.getDialog("insertimage");
		myImage.open();
		_editor.addListener('beforeInsertImage', function (type,data) {     //侦听图片上传
			$(obj).parent('span').siblings('input').val(data[0].src);     //将地址赋值给相应的input
		});
	}
	function upFiles(obj) {
		var myFiles = _editor.getDialog("attachment");
		myFiles.open();
		_editor.addListener('beforeinsertfile', function (type, data) {
			$(obj).parent('span').siblings('input').val(data[0].url);     //将地址赋值给相应的input
		});
	}
	function upImages(obj){
		
		var myImage = _editor.getDialog("insertimage");
		
		myImage.open();
		_editor.addListener('beforeInsertImage', function (t, arg) {     //侦听图片上传
			$.each(arg,function(i,rs){
				var text = "";
				text     += '<li>'
				text     += '	<img src="'+rs+'" class="img-polaroid">'
				text     += '	<input type="hidden" value="'+rs+'" name="content[]">'
				text     += '	<span class="error" onClick="removefile(this)"></span>'
				text     += '</li>'
				$(obj).siblings('.img_list_box').find('ul').append(text);
			})
		});
	}
	
	$('button[data-upload=images],span[data-upload=images]').click(function(){
		upImages(this);
	});
	$('button[data-upload=image],span[data-upload=image]').click(function(){
		upImage(this);
	});
	$('button[data-upload=files]').click(function(){
		upFiles(this);
	});
}